package com.hz.controller;

import com.hz.controller.QrCodeController;
import com.hz.pojo.WashMachine;
import com.hz.service.WashMachineService;
import com.hz.utils.QrCodeUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;

/**
 * @author novel
 * @date 2020/8/24
 */
@RestController
@RequestMapping("/QrCodeController")
public class QrCodeController {

    /**
     * 二维码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/qrcode")
    public void qrcode(@RequestBody HashMap<String,Object> map,HttpServletRequest request, HttpServletResponse response) {
        StringBuffer url = request.getRequestURL();
        //获取本机id真实地址
        InetAddress ip4 = null;
        try {
            ip4 = Inet4Address.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String ip = ip4.getHostAddress();
        System.out.println(ip);
        // 域名
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
        // 再加上请求链接
        /*String requestUrl = tempContextUrl + "index.html";*/
        System.out.println(map.get("user_id"));
        //给二维码设置参数
        String requestUrl = "http://"+ip+"/bindPhone.html?user_pid="+map.get("user_id")+"";
//        String requestUrl = "http://moyuhz.top:8080/templates/bindPhone.html?user_pid="+map.get("user_id")+"";


       /* String requestUrl = "http://192.168.43.193:5500/bindPhone.html?user_pid="+map.get("user_id")+"";*/
        System.out.println(requestUrl);

        try {
            OutputStream os = response.getOutputStream();
            QrCodeUtils.encode(requestUrl, os);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 二维码
     * @param request
     * @param response
     */
    @RequestMapping("/qrcode2")
    public void qrcode2( HttpServletRequest request, HttpServletResponse response) {
        StringBuffer url = request.getRequestURL();
        // 域名
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();

        // 再加上请求链接
        String requestUrl = tempContextUrl + "index.html";
        try {
            OutputStream os = response.getOutputStream();
            QrCodeUtils.encode(requestUrl, "/static/images/logo.jpg", os, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
